import os
import time
import json


def build_session_data(username, session_expire):
    # 用构建session数据，返回一个json字符串
    now = int(time.time())
    session_data = {'username': username, 'expire': now + session_expire}
    return json.dumps(session_data)


def write_session_data(sid, session_data_json):
    # 确保session数据的存放目录存在
    os.makedirs(session_dir, exist_ok=True)

    # 构建session文件路径，并打开
    session_file = os.path.join(session_dir, sid)
    ofile = open(session_file, 'w')
    ofile.write(session_data_json)
    ofile.close()

session_dir = '/tmp/session_data'
session_expire = 86400
redirect_url = '/cgi/monitor9'
sid_length = 40
